﻿using DataShow.codeModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace DataShow.codeBLL
{
    public class WorkBLL
    {
        #region 分析上传文件
        /// <summary>
        /// 分析上传文件
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public bool AnalyseTXT(string content)
        {
            bool successs = false;
            WorkItem wi = null;
            string[] lines = content.Split(new string[] {"\r\n" },StringSplitOptions.None );
            int totalCount=0;
            foreach (var item in lines)
            {
                string[] values = item.Split(' ');
                if (values.Count() != 10)
                {
                    continue;
                }
                string point = values[1] + (values[2].Length == 1 ? ("0" + values[2]) : values[2]) + (values[3].Length == 1 ? ("0" + values[3]) : values[3]);
                try
                {
                    wi = new WorkItem() { Station = values[0], Clock = "H" + values[4], Forecastnumber = values[9], Time = point, SendPoint = "H" + values[8], SpanKind = "sm" };
                    successs = new codeDAL.WorkProcess().AddSchedule(wi.Time, wi.Station, wi.Clock, wi.SpanKind, wi.SendPoint, wi.Forecastnumber);
                    totalCount++;
                }
                catch
                {
                    System.Web.HttpContext.Current.Session.Add("sheetcout",totalCount);
                    return false;
                }
                
            }
            return successs;
        } 
        #endregion

        #region 搜索排班表
        /// <summary>
        /// 搜索排班表
        /// </summary>
        /// <param name="pagesize">页容量</param>
        /// <param name="pageindex">页码</param>
        /// <returns></returns>
        public DataTable Search(int pagesize, int pageindex)
        {
            return new DataShow.codeDAL.WorkProcess().Search(pageindex, pagesize);
        } 
        #endregion

        #region 删除一条记录
        /// <summary>
        /// 删除一条记录
        /// </summary>
        /// <param name="id">记录ID</param>
        /// <returns></returns>
        public bool Delete(List<string> ids)
        {
            bool flag=true;
            codeDAL.WorkProcess dal = new codeDAL.WorkProcess();
            foreach (var item in ids)
            {
                flag = dal.DeleteSchedule(Convert.ToInt32(item));
            }
            return flag;
        } 
        #endregion

        #region 计算总数
        /// <summary>
        /// 计算总数
        /// </summary>
        /// <returns></returns>
        public int CalculateCount()
        {
            string sql = "select id from New_Schedule";
            return new codeDAL.SqlHelper().ExecuteQuery(sql, CommandType.Text).Rows.Count;
        } 
        #endregion
    }
}